from django.http import HttpResponseRedirect
from django.shortcuts import render
from . import db, tool
from datetime import datetime


# Create your views here.

def index(request):
    return render(request, 'common/index.html')


def to_index(request):
    return HttpResponseRedirect('/index')


# 设置手机号的验证码
def set_verification_code(request):
    phone = request.POST.get('phone')
    code = request.POST.get('code')
    if not phone or not code:
        return render(request, 'common/result.html', context=tool.msg('参数错误'))
    # 判断长度
    if len(phone) != 11 or len(code) != 4:
        return render(request, 'common/result.html', context=tool.msg('手机号或验证码长度不对'))
    if phone and code:
        try:
            db.set_redis_value(f'shai_main:verify_code:{phone}', f'\"{code}\"')
            return render(request, 'common/result.html', context=tool.msg(f'{phone} 验证码设置为 {code} 成功'))
        except:
            return render(request, 'common/result.html', context=tool.msg('设置失败'))


# 设置用户资料已修改次数
def set_userinfo_times(request):
    shaicode = request.POST.get('shaicode')
    nickname = request.POST.get('nickname')
    real_name = request.POST.get('real_name')
    intro = request.POST.get('intro')
    avatar = request.POST.get('avatar')

    if len(shaicode) != 8:
        return render(request, 'common/result.html', context=tool.msg('晒code不正确'))

    try:
        r = db.get_redis_db()
        if nickname:
            r.hset(f'shai_main:modify_info:{shaicode}', 'nickname', nickname)
        if real_name:
            r.hset(f'shai_main:modify_info:{shaicode}', 'real_name', real_name)
        if intro:
            r.hset(f'shai_main:modify_info:{shaicode}', 'intro', intro)
        if avatar:
            r.hset(f'shai_main:modify_info:{shaicode}', 'avatar', avatar)

        return render(request, 'common/result.html', context=tool.msg(f'晒code {shaicode} 重置成功'))
    except:
        return render(request, 'common/result.html', context=tool.msg('设置失败'))


# 删除绑定的手机号
def unbind_phone(request):
    phone = request.POST.get('phone')
    if not phone:
        return render(request, 'common/result.html', context=tool.msg('参数错误'))
    # 判断长度
    if len(phone) != 11:
        return render(request, 'common/result.html', context=tool.msg('手机号长度不对'))
    try:
        db.mysql(sql=f'update user_info SET phone=NULL WHERE phone = "{phone}";')
        return render(request, 'common/result.html', context=tool.msg(f'{phone} 解除绑定成功'))
    except:
        return render(request, 'common/result.html', context=tool.msg('解除绑定失败'))


# 发布多条评论
def comment(request):
    # 藏品编号
    sn = request.POST.get('sn')
    # 插入的条数
    nums = request.POST.get('nums')
    # 评论的点赞数
    likes = request.POST.get('likes')
    # 评论的内容
    content = request.POST.get('content')
    # 类型 0 评论 1问答
    type = request.POST.get('type')
    # 藏品id
    sn_id = db.mysql(f'SELECT id FROM product WHERE sn = "{sn}";', db='shai_bbs')
    if not sn_id:
        return render(request, 'common/result.html', context=tool.msg('藏品不存在'))
    sn_id = sn_id[0][0]
    # 参与评论的用户
    user_ids = db.mysql(f'SELECT id FROM user_info ORDER BY id DESC LIMIT {nums};')
    user_ids = [u[0] for u in user_ids]
    try:
        for i in range(int(nums)):
            now = str(datetime.now())
            user_id = user_ids.pop()
            db.mysql(
                f'INSERT INTO product_comment ( product_id, type, user_id, content, liked, commented, status, create_time, update_time, delete_time, is_delete)VALUES( {sn_id}, {type}, {user_id}, "{content}", {likes}, 0, 1, "{now}", "{now}", NULL, 0 );',
                db='shai_bbs')
        return render(request, 'common/result.html', context=tool.msg('评论完毕'))
    except:
        return render(request, 'common/result.html', context=tool.msg('评论失败'))


def login(request):
    # 获取请求方法
    # print(request.method)
    # print(request.path)
    # print(request.content_type)
    # print(request.headers)
    # print(request.COOKIES)
    # print(request.get_host())
    # print(request.body)
    # 获取get的参数
    print(request.GET.get('a'))
    # 获取post的参数 使用x-www-form-urlencoded 或 form-data
    print(request.POST.get('x'))
    # 处理json请求
    # print(json.loads(request.body))
    return render(request, 'common/result.html', context={'msg': '123'})
